import React, {Component} from 'react'
import {fetchData} from '../../../components/AjaxConsumer'
import {
    Form,
    Input,
    Tooltip,
    Icon,
    Cascader,
    Select,
    Row,
    Col,
    Checkbox,
    Button,
    message,
    Modal,
    Radio,
    InputNumber
} from 'antd';
import { SelectUrl } from '../../../components';
const FormItem = Form.Item;

class WorkAreaForm extends React.Component {
    state = {
        loading: false,
        isEdit: false,
        submitUrl: '',
        regionCodeUrl: '',
        modelTypeUrl:'/base/dic/get/model_type',
        typeUrl:'/base/dic/get/type',
        contractUrl:'/base/data/contract/get',
        formData: {}
    };

    handleSubmit = (e) => {
        e.preventDefault();
        console.log('44444444444-----------');
        this.props.form.validateFieldsAndScroll((err, fieldsValue) => {
            if (!err) {
                this.setState({loading: true});
                let values = {
                    ...fieldsValue,
                };
                console.log('表单提交数据-----------', values);
                //发送ajax 控制请求数据规则，无值的设为空
                for (let item in values) {
                    if (!values[item]) {
                        values[item] = ''
                    }
                }
                const options = {
                    url: this.state.submitUrl,
                    type: 'post',
                    condition: values
                };

                fetchData(
                    this,
                    options,
                    (res) => {
                        //成功回调函数
                        this.setState({
                            loading: false
                        });
                        if (res.code == '1') {
                            message.success('操作成功');

                            this.handleCancel();
                            this.props.refreshHandle();
                        } else {
                            //message.error(res.msg);
                        }

                    },
                    (res) => {
                        this.setState({
                            loading: false
                        });
                    }
                )
            }
        });
    }

    handleCancel = () => {
        this.props.onCancel();//123
        this.props.form.resetFields(); //初始化表单
        //初始化ajax加载过来的selectList
    }

    //propertity 修改时执行
    componentWillReceiveProps(newProps){
        this.setState({
            isEdit: newProps.isEdit,
            submitUrl: newProps.submitUrl,
            formData: newProps.formData
        });
    }

    // 组件参数实例化-仅一次
    componentWillMount(){
        this.setState({
            isEdit: this.props.isEdit,
            submitUrl: this.props.submitUrl,
            formData: this.props.formData
        });
    }

    render() {
        const {getFieldDecorator} = this.props.form;
        const formItemLayout = {
            labelCol: {
                xs: {span: 24},
                sm: {span: 6}
            },
            wrapperCol: {
                xs: {span: 24},
                sm: {span: 18}
            }
        };
        const tailFormItemLayout = {
            wrapperCol: {
                xs: {span: 24, offset: 0},
                sm: {span: 24, offset: 10}
            }
        };

        let objectInfo = this.state.formData;

        return (
            <div>
                <Form
                    onSubmit={this.handleSubmit}
                    style={{
                                backgroundColor: '#fff',
                                paddingTop: '20px',
                                paddingBottom: '20px'
                            }}>
                    <Row>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="型号类型">
                                {getFieldDecorator('modelType', {
                                    initialValue: objectInfo.modelType ? objectInfo.modelType : "",
                                    rules: [
                                        {
                                        }
                                    ]
                                })(
                                    <SelectUrl url={this.state.modelTypeUrl} isDisable="ture" type="get" dicCode="dicCode" dicName="dicName"/>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="型号代码">
                                {getFieldDecorator('model', {
                                    initialValue: objectInfo.model ? objectInfo.model : "",
                                    rules: [
                                      {
                                        }
                                    ]
                                })(
                                    <Input disabled="true" placeholder="请输入"/>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="型号描述">
                                {getFieldDecorator('modelDesc', {
                                    initialValue: objectInfo.modelDesc ? objectInfo.modelDesc : "",
                                    rules: [
                                        {
                                        },{
                                            max:100,message: '输入内容应少于100个字符'
                                        }
                                    ]
                                })(
                                    <Input disabled="true" placeholder="请输入"/>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="库位">
                                {getFieldDecorator('wlCode', {
                                    initialValue: objectInfo.wlCode ? objectInfo.wlCode : "",
                                    rules: [
                                        {
                                        }
                                    ]
                                })(
                                    <Input disabled="true" placeholder="请输入"/>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="原合同号">
                                {getFieldDecorator('contractNoOld', {
                                    initialValue: objectInfo.contractNo ? objectInfo.contractNo : "",
                                    rules: [
                                        {
                                        }
                                    ]
                                })(
                                    <SelectUrl url={this.state.contractUrl} isDisable="ture" type="get" dicCode="contractNo" dicName="contractNo"/>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="新合同号">
                                {getFieldDecorator('contractNo', {
                                    rules: [
                                        {
                                            required: true, whitespace: true, message: '请输入新合同号'
                                        }
                                    ]
                                })(
                                    <SelectUrl url={this.state.contractUrl} type="get" dicCode="contractNo" dicName="contractNo"/>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="可用个数">
                                {getFieldDecorator('availAmount', {
                                    initialValue: objectInfo.availNum ? objectInfo.availNum.toString() : "0",
                                    rules: [
                                        {
                                        }
                                    ]
                                })(
                                    <InputNumber disabled="true" min={0} max={9999} step={1}/>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="修改个数">
                                {getFieldDecorator('modiAmount', {
                                    initialValue: '',
                                    rules: [
                                        {
                                            required: true, message: '请输入修改个数'
                                        }
                                    ]
                                })(
                                    <InputNumber min={1} max={9999} step={1}/>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={24} style={{ textAlign: 'center',marginTop:'10px' }}>
                                <Button type="primary" htmlType="submit" size="large">
                                    {this.state.loading ? '提交中...' : '确定'}
                                </Button>
                                <Button type="default" style={{marginLeft: '50px'}} size="large" onClick={this.handleCancel}>
                                    取消
                                </Button>
                        </Col>
                    </Row>
                </Form>
            </div>
        );
    }
}

export default Form.create()(WorkAreaForm);
